package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import model.CostumerModel;
import model.Envelope;
import client.CcrmApp;
import view.InsertPotentialGui;
/**
 * This Class is a Insert Potential Controller, which extends AbstractTransfer
 * this class search non Potential customer with rating 0, interest level - 0, and set him to "Potential customer"
 * @author jacob
 *
 */
public class InsertPostentialController extends AbstractTransfer {
	private InsertPotentialGui InsertPotView;
	private MarketingController prevCon;
	private ArrayList<CostumerModel> listCostomer;
	/**
	 * 
	 * @param InsPotview is Insert Potential Gui instance
	 * @param prevCont is previous controller instance 
	 */
	public InsertPostentialController(InsertPotentialGui InsPotview ,MarketingController prevCont ){
		InsertPotView = InsPotview;
		prevCon = prevCont;
		InsertPotView.addBackBottonCustomerreportActionListener(new BackListener());
		
		checkDB();
	}

 private void checkDB() { //check DB if you need to add some Potential customer
	listCostomer = new ArrayList<CostumerModel>();
	Envelope ev = new Envelope(listCostomer, "Potential Customer");
	CcrmApp.clien.setCurrObj(this);
	sendToServer(ev);
	}
/**
 * Inner class that handles when Button back Pressed, implements ActiontListener
 * @author jacob
 *
 */
	class BackListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				InsertPotView.dispose();
				prevCon.getMarkGui().setVisible(true);
				
			}
			  
		 }

	 @SuppressWarnings("unchecked")
	 @Override
	 /*
	  * (non-Javadoc)
	  * @see controllers.AbstractTransfer#handleDBResult(java.lang.Object)
	  */
	public void handleDBResult(Object message) {
     Envelope ev1 = (Envelope)message;
     String str = new String("");
     //set the potential customer list the come from server on Gui TextArea
     if(ev1.getMess().equals("change to potential")){
    	 str+="potential Customer Successfully added:\n\n";
    		for(int i = 0; i < ((ArrayList<CostumerModel>)ev1.getObject()).size() ; i++){
				 str+= "Customer ID : " + (((ArrayList<CostumerModel>)ev1.getObject()).get(i).getUid()) + ", Customer Name: "
				 		+ "  " + (((ArrayList<CostumerModel>)ev1.getObject()).get(i).getFirstname()) +"  "
				 				+ "" +(((ArrayList<CostumerModel>)ev1.getObject()).get(i).getLasttname()) + "\n"; 

			}
    		InsertPotView.getTextArea().setText(str); 
     }
     // do not find potential Customer to add
  if(ev1.getMess().equals("not found potential")){
	  str+="Can not find potential Customer to add";
	  InsertPotView.getTextArea().setText(str);  
     }
     
	 }
	
}
